package com.youmans.staService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youmans.commonutils.R;
import com.youmans.staService.client.UcenterClien;
import com.youmans.staService.entity.StatisticsDaily;
import com.youmans.staService.mapper.StatisticsDailyMapper;
import com.youmans.staService.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Youmans
 * @since 2022-05-24
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClien ucenterClien ;
    @Override
    public void registerCount(String day) {
        //添加之前删除相同条件数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>() ;
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper) ;

        R r = ucenterClien.countRegister(day);
//        Integer count = (Integer) r.getData().get("countRegister");666
        Integer count = (Integer) r.getData().get("countRegister");
        //把获取数据添加数据库，统计分析表里面
        StatisticsDaily statisticsDaily = new StatisticsDaily();
//        注册人数
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setDateCalculated(day);//统计日期

        //TODO 远程调用查询登录人数，观看视频数，新增课程数
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200)) ;
        baseMapper.insert(statisticsDaily) ;
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>() ;
        wrapper.between("date_calculated",begin,end) ;
        wrapper.select("date_calculated",type);//选择查询
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper) ;

        List<String> date_calculatedList = new ArrayList<>() ;

        List<Integer> numDataList = new ArrayList<>() ;


        switch (type){
            case "login_num" :
                for (StatisticsDaily statisticsDaily : statisticsDailies) {
                    date_calculatedList.add(statisticsDaily.getDateCalculated()) ;
                numDataList.add(statisticsDaily.getLoginNum()) ;}
                break;
            case "video_view_num" :
                for (StatisticsDaily statisticsDaily : statisticsDailies) {
                    date_calculatedList.add(statisticsDaily.getDateCalculated()) ;
                numDataList.add(statisticsDaily.getVideoViewNum());}
                break;
            case "course_num" :
                for (StatisticsDaily statisticsDaily : statisticsDailies) {
                    date_calculatedList.add(statisticsDaily.getDateCalculated()) ;
                numDataList.add(statisticsDaily.getCourseNum()) ;}
                break;
            case "register_num" :
                for (StatisticsDaily statisticsDaily : statisticsDailies) {
                    date_calculatedList.add(statisticsDaily.getDateCalculated()) ;
                numDataList.add(statisticsDaily.getRegisterNum());}
                break;
            default:
                break;
        }
        Map<String,Object> map = new HashMap<>() ;
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
